﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.BLL;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;

namespace SalesSys.BLL
{
    public class NoticeInfoBLL : BaseBLL<NoticeInfo, Guid>
    {
        /// <summary>
        /// 发送一个消息，
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="to">接受人,为null表示没有</param>
        /// <param name="toRoles">接收角色，</param>
        public void SendMsg(string msg, Guid to, string[] toRoles)
        {
            //消息发送待实现
            NoticeInfo info = new NoticeInfo();
            info.Content = msg;
            info.CreateTime = DateTime.Now;
            info.To = to;
            if (toRoles == null)
            {
                info.ToRoles = null;
            }
            else
            {
                info.ToRoles = string.Join("|", toRoles);                 
            }
            BllFactory.GetBll<NoticeInfoBLL>().Create(info);
        }

        public KeyValuePair<int, IEnumerable<NoticeInfo>> Select(string content, IEmployee employee, QueryInfo qinfo)
        {
            var query = Rep.Entities;
            if (!string.IsNullOrWhiteSpace(content))
            {
                query = query.Where(p => p.Content.Contains(content));
            }

            //if (null != employee)
            //{
            //    query = query.Where(p => p.To.Equals(employee.Id) || p.ToRoles.Contains(employee.Roles));
            //}

            var count = query.Count();
            var list = query.OrderByDescending(p => p.CreateTime).Paging(qinfo.Page, qinfo.PageSize).ToList();
            return new KeyValuePair<int, IEnumerable<NoticeInfo>>(count, list);
        }
        /// <summary>
        /// 获取大于指定时间的所有推送消息列表
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="maxCount"></param>
        /// <returns></returns>
        public  NoticeInfo[] GetPushMsg(DateTime dt,int maxCount=10)
        {
            return  Rep.Entities.Where(p => p.CreateTime > dt).OrderBy(p => p.CreateTime).Take(maxCount).ToArray();
        }
     
    }
}
